/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.droppable;

import org.apache.wicket.Component;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQDroppableUtils
{

	/**
	 * Create a JQDroppableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQDroppableBuilder
	 */
	public static JQDroppableBuilder create(String id, String key)
	{
		return new JQDroppableBuilder(new JQDroppable(id, (key == null ? JQDroppablePlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQDroppableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQDroppableBuilder
	 */
	public static JQDroppableBuilder create(String id)
	{
		return new JQDroppableBuilder(new JQDroppable(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQDroppableBuilder
	{
		/** JQuery component */
		private JQDroppable component;
		/** JQuery default options */
		private JQDroppableOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQDroppableBuilder(JQDroppable component)
		{
			this.component = component;
			this.options = new JQDroppableOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the accept option value
		 * 
		 * @param accept
		 *            droppable accept
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder accept(String accept)
		{
			options.setAccept(accept);
			return this;
		}

		/**
		 * Sets the accept option value
		 * 
		 * @param accept
		 *            droppable accept
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder accept(Component accept)
		{
			options.setAccept(accept);
			return this;
		}

		/**
		 * Sets the active class option value
		 * 
		 * @param activeClass
		 *            droppable active class
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder activeClass(String activeClass)
		{
			options.setActiveClass(activeClass);
			return this;
		}

		/**
		 * Sets the greedy option value
		 * 
		 * @param greedy
		 *            droppable greedy
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder greedy(boolean greedy)
		{
			options.setGreedy(greedy);
			return this;
		}

		/**
		 * Sets the hover class option value
		 * 
		 * @param hoverClass
		 *            droppable hover class
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder hoverClass(String hoverClass)
		{
			options.setHoverClass(hoverClass);
			return this;
		}

		/**
		 * Sets the tolerance option value
		 * 
		 * @param tolerance
		 *            droppable tolerance
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder tolerance(String tolerance)
		{
			options.setTolerance(tolerance);
			return this;
		}

		/**
		 * Sets the activate function
		 * 
		 * @param activateScript
		 *            droppable activate function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder activateScript(String activateScript)
		{
			options.setActivateScript(activateScript);
			return this;
		}

		/**
		 * Sets the deactivate function
		 * 
		 * @param deactivateScript
		 *            droppable deactivate function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder deactivateScript(String deactivateScript)
		{
			options.setDeactivateScript(deactivateScript);
			return this;
		}

		/**
		 * Sets the accept function
		 * 
		 * @param acceptScript
		 *            droppable accept function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder acceptScript(String acceptScript)
		{
			options.setAcceptScript(acceptScript);
			return this;
		}

		/**
		 * Sets the over function
		 * 
		 * @param overScript
		 *            droppable over function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder overScript(String overScript)
		{
			options.setOverScript(overScript);
			return this;
		}

		/**
		 * Sets the out function
		 * 
		 * @param outScript
		 *            droppable out function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder outScript(String outScript)
		{
			options.setOutScript(outScript);
			return this;
		}

		/**
		 * Sets the drop function
		 * 
		 * @param dropScript
		 *            droppable drop function
		 * @return JQDroppableBuilder
		 */
		public JQDroppableBuilder dropScript(String dropScript)
		{
			options.setDropScript(dropScript);
			return this;
		}

		/**
		 * @return JQDroppable
		 */
		public JQDroppable build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}